package com.leyou.page.service.impl;

import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.client.ItemClient;
import com.leyou.item.dto.*;
import com.leyou.page.service.GoodsPageService;
import com.leyou.page.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 付
 */
@Service
public class GoodsPageServiceImpi  implements GoodsPageService {
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private StringRedisTemplate redisTemplate;
    private static final String SPU_KEY_PREFIX="page:spu:id:";
    private static final String DETAIL_KEY_PREFIX = "page:detail:id:";
    private static final String SKU_KEY_PREFIX = "page:sku:id:";
    private static final String CATEGORY_KEY_PREFIX = "page:category:id:";
    private static final String BRAND_KEY_PREFIX = "page:brand:id:";
    private static final String SPEC_KEY_PREFIX = "page:spec:id:";
    /**
     * 加载spu到redis中并返回
     */
    @Override
    public String loadSpuData(Long spuId) {
        //远程查询数据
        SpuDTO spuDTO = itemClient.querySpuById(spuId);
        //将查询到的数据转换为vo
        SpuVO spuVO = spuDTO.toEntity(SpuVO.class);
        spuVO.setCategoryIds(spuDTO.getCategoryIds());
        //写入redis中
        String json = JsonUtils.toJson(spuVO);
        redisTemplate.opsForValue().set(SPU_KEY_PREFIX+spuId,json);
        //返回
        return json;
    }
    /**
     * 加载spuDetail到redis中并返回
     */
    @Override
    public String loadSpuDetailData(Long spuId) {
        //远程查询数据
        SpuDetailDTO detail = itemClient.querySpuDetailById(spuId);
        //将查询到的数据转换为vo
        String json = JsonUtils.toJson(detail);
        //写入redis中
        redisTemplate.opsForValue().set(DETAIL_KEY_PREFIX+spuId,json);
        //返回
        return json;
    }
    /**
     * 加载sku信息到redis中并返回
     */
    @Override
    public String loadSkuListData(Long spuId) {
        //远程查询数据
        List<SkuDTO> skulist = itemClient.querySkuBySpuId(spuId);
        //将查询到的数据转换为vo
        String json = JsonUtils.toJson(skulist);
        //写入redis中
      redisTemplate.opsForValue().set(SKU_KEY_PREFIX+spuId,json);
        //返回
        return json;
    }
    /**
     * 加载分类信息到redis中并返回
     */
    @Override
    public String loadCategoriesData(List<Long> ids) {
        if (ids.size()!=3){
            throw new LyException(400,"商品分类需要3级id");
        }
        //远程查询数据
        List<CategoryDTO> dtoList = itemClient.queryCategoryByIds(ids);
        List<CategoryVO> collect = dtoList.stream().map(categoryDTO -> categoryDTO.toEntity(CategoryVO.class)).collect(Collectors.toList());
        //将查询到的数据转换为vo
        String json = JsonUtils.toJson(collect);
        //写入redis中
        redisTemplate.opsForValue().set(CATEGORY_KEY_PREFIX+ids.get(2),json);
        //返回
        return json;
    }
    /**
     * 加载品牌信息到redis中并返回
     */
    @Override
    public String loadBrandData(Long brandId) {
        //远程查询数据
        BrandDTO brandDTO = itemClient.queryBrandById(brandId);
        BrandVO brandVO = brandDTO.toEntity(BrandVO.class);
        //将查询到的数据转换为vo
        String json = JsonUtils.toJson(brandVO);
        //写入redis中
        redisTemplate.opsForValue().set(BRAND_KEY_PREFIX+brandDTO,json);
        //返回
        return json;
    }
    /**
     * 加载规格参数信息到redis中并返回
     */
    @Override
    public String loadSpecData(Long categoryId) {
        //远程查询数据
        List<SpecGroupDTO> groupDTOList = itemClient.querySpecList(categoryId);
        List<SpecGroupVO>groupVOS=new ArrayList<>();
        for (SpecGroupDTO groupDTO : groupDTOList) {
            SpecGroupVO specGroupVO = new SpecGroupVO();
            //设置name
            specGroupVO.setName(groupDTO.getName());
            //获取params
            List<SpecParamDTO> params = groupDTO.getParams();
            //转换为specvo
            List<SpecParamVO> collect = params.stream().map(paramDTO -> paramDTO.toEntity(SpecParamVO.class)).collect(Collectors.toList());
            //添加params
            specGroupVO.setParams(collect);
            groupVOS.add(specGroupVO);
        }

        //将查询到的数据转换为vo
        String json = JsonUtils.toJson(groupVOS);
        //写入redis中
        redisTemplate.opsForValue().set(SPEC_KEY_PREFIX+categoryId,json);
        //返回
        return json;
    }

    //主从删除测试
    @Override
    public void deleteSkuList(Long spuId) {
        redisTemplate.delete(SKU_KEY_PREFIX+spuId);
    }
}
